package com.maxd.modules.coupon.controller;

import com.gexin.fastjson.JSON;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.coupon.entity.CouponOrderDetail;
import com.maxd.modules.coupon.service.CouponOrderDetailService;
import com.maxd.modules.coupon.utils.CouponOrderDetailUtil;
import com.maxd.modules.syslog.entity.RequestLogs;
import com.maxd.modules.syslog.service.RequestLogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/14 18:37
 * @Description:
 */
@RestController
@Api(value="用户订单优惠明细管理",tags={"用户订单优惠明细"})
@RequestMapping(value = "/couponOrderDetail")
public class CouponOrderDetailController {

    private final static Logger log = LoggerFactory.getLogger(CouponOrderDetailController.class);

    @Autowired
    private CouponOrderDetailService couponOrderDetailService;

    @Autowired
    private RequestLogsService requestLogsService;

    CouponOrderDetailUtil couponOrderDetailUtil = new CouponOrderDetailUtil();

    /**
     * 分页查询用户订单优惠明细
     */
    @RequestMapping(value = "/findAllCouponOrderDetail", method = RequestMethod.POST)
    @ApiOperation("查询用户订单优惠明细列表")
    //@IgnoreSecurity
    public Result findAllCouponOrderDetail(@RequestBody CouponOrderDetail couponOrderDetail, @RequestParam Integer page, @RequestParam int size) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "findAllCouponOrderDetail",null,
                "/couponAmount/findAllCouponOrderDetail", JSON.toJSONString(couponOrderDetail));
        requestLogsService.saveRequestLogs(requestLogs);

        CouponOrderDetail couponOrderDetail1 = couponOrderDetailUtil.copyNull(couponOrderDetail);
        Pageable pageable = PageRequest.of(page, size);
        return couponOrderDetailService.findAll(couponOrderDetail1, pageable);
        //todo 最好写构造器 之后直接构造器查询
    }

    /**
     * todo save
     */

    /**
     * 修改用户订单优惠明细 根据id
     */
    /**
     * 根据id修改
     */
    @RequestMapping(value = "/updateCouponOrderDetail", method = RequestMethod.PUT)
    @ApiOperation("根据id修改")
    //@IgnoreSecurity
    public Result updateCouponOrderDetail(@RequestBody CouponOrderDetail couponOrderDetail) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "updateCouponOrderDetail",null,
                "/couponAmount/updateCouponOrderDetail", JSON.toJSONString(couponOrderDetail));
        requestLogsService.saveRequestLogs(requestLogs);

        log.info(couponOrderDetail.getId()+"");
        if(couponOrderDetail==null){
            return ResultUtil.error(601,"修改失败,请稍后再试");
        }
        try {
            couponOrderDetailService.updateCouponOrderDetailById(couponOrderDetail);
        }catch (Exception e){
            return ResultUtil.error(602,"修改失败,请稍后再试或联系管理员");
        }

        return ResultUtil.success("修改成功");
    }



    /**
     * 根据id删除
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation("删除卡券")
    public Result deleteById(@RequestParam Long id) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "deleteById",null,
                "/couponAmount/deleteById", String.valueOf(id));

        couponOrderDetailService.deleteById(id);
        return ResultUtil.success();
    }

    /**
     * 根据ids删除
     */
    @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    @ApiOperation("删除多个卡券")
    public Result deleteByIdList(@RequestParam String ids) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "deleteByIdList",null,
                "/couponAmount/deleteByIdList", String.valueOf(ids));

        couponOrderDetailService.deleteByIdList(ids);
        return ResultUtil.success();
    }



}
